package com.netease.nie.config;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.netease.nie.base.exceptions.UnexpectedException;

@Component
@ConfigurationProperties(prefix = "credit", locations = "classpath:config/credit-grade.properties")
public class CreditGradeProperties {

	private List<String> vips = new ArrayList<String>();
	
	public List<String> getVips() {
		return vips;
	}

	public void setVips(List<String> vips) {
		this.vips = vips;
	}
	
	public List<Integer> getLevelRange(int level) {
		String vipLevels = vips.get(level);
		if (StringUtils.isNotBlank(vipLevels)) {
			List<Integer> vipLevelRangeList = new ArrayList<>();
			Integer floor = Integer.valueOf(vipLevels.split(",").length == 2 ? vipLevels.split(",")[0] : vipLevels);
			Integer ceil = Integer.valueOf(vipLevels.split(",").length == 2 ? vipLevels.split(",")[1] : vipLevels);
			vipLevelRangeList.add(floor);
			vipLevelRangeList.add(ceil);
			return vipLevelRangeList;
		}
		
		throw new UnexpectedException("credit grade over limit");
	}
	
	public Integer getLevel(int gradeCredit) {
		int vipSize = vips.size();
		List<Integer> vipLevelRangeList = null;
		for (int i = 0; i < vipSize; i++) {
			vipLevelRangeList = getLevelRange(i);
			if (gradeCredit < vipLevelRangeList.get(1)) {
				return i;
			}
		}
		return --vipSize;
	}
	
}
